﻿using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Zws.Common.ScanTool;
using Zws.Frameworker.Module;

namespace Zws.AutoMapper
{
    public class ZwsAutoMapperModule : ModuleMark
    {
        public void OnBuilderExec(WebApplicationBuilder builder)
        {
            var mapperConfig = new MapperConfiguration(mc =>
            {
                var profileTypes = ScanHelper.ScanSpecialPublicClassByAssemblys(x =>
                x.IsAssignableTo(typeof(ProfileMark)) && !x.IsAbstract);
                foreach (var type in profileTypes)
                {
                    mc.AddProfile(type);
                }
            });
            IMapper mapper = mapperConfig.CreateMapper();
            builder.Services.AddSingleton(mapper);
        }
    }
}